//
//  NSString+AES.m
//  ElectronicSwitch
//
//  Created by 陈友文 on 2019/2/22.
//  Copyright © 2019年 陈友文. All rights reserved.
//

#import "NSString+AES.h"
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCryptor.h>





@implementation NSString (AES)

-(NSString *)AESForSting:(NSString *)str{

    NSData *dataStr = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableData *data = dataStr.mutableCopy;
    Byte a = 0xff;
    for (NSInteger i = 0; i < 4; i++) {
        [data appendBytes:&a length:sizeof(a)];
    }
    
    return  [self stringFromData:[self encryptDataWithData:data]];
    
}

-(NSData *)encryptDataWithData:(NSData *)data{
    Byte b1 = 0x73;
    Byte b2 = 0x65;
    Byte b3 = 0x6e;
    Byte b4 = 0x73;
    Byte b5 = 0x6f;
    Byte b6 = 0x72;
    Byte b7 = 0x36;
    Byte b8 = 0x36;
    Byte b9 = 0x38;
    Byte b10 = 0xff;
    Byte b11 = 0xff;
    Byte b12 = 0xff;
    Byte b13 = 0xff;
    Byte b14 = 0xff;
    Byte b15 = 0xff;
    Byte b16 = 0xff;
    Byte key[] = {b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16};
    
    char keyPtr[kCCKeySizeAES128 + 1];
    bzero(keyPtr, sizeof(keyPtr));
    //    [key getCString:keyPtr maxLength:sizeof(key) encoding:NSUTF8StringEncoding];
    NSUInteger dataLength = sizeof(key);
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                          
                                          kCCAlgorithmAES128,
                                          
                                          kCCOptionPKCS7Padding | kCCOptionECBMode,
                                          
                                          key,
                                          
                                          kCCBlockSizeAES128,
                                          
                                          NULL,
                                          
                                          [data bytes],
                                          
                                          dataLength,
                                          
                                          buffer,
                                          
                                          bufferSize,
                                          
                                          &numBytesEncrypted);
    
    if(cryptStatus == kCCSuccess)
    {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }
    free(buffer);
    
    return nil;
    
}



-(NSString *)stringFromData:(NSData *)result{
    if(result && result.length > 0)
        
    {
        
        Byte *datas = (Byte *)[result bytes];
        
        NSMutableString *outPut = [NSMutableString stringWithCapacity:result.length];
        
        for(int i = 0 ; i < result.length ; i++)
            
        {
            
            [outPut appendFormat:@"%02x",datas[i]];
            
        }
        if (outPut.length > 16) {
            
            return [outPut substringWithRange:NSMakeRange(0, 16)];
        }
        
        
    }
    
    return nil;
    
}

//加密
+(NSString *)AES128Encrypt:(NSString *)plainText key:(NSString *)key
{
    char keyPtr[kCCKeySizeAES128+1];
    memset(keyPtr, 0, sizeof(keyPtr));
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    
    NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];
    NSUInteger dataLength = [data length];
    
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                          kCCAlgorithmAES128,
                                          kCCOptionPKCS7Padding|kCCOptionECBMode,
                                          keyPtr,
                                          kCCBlockSizeAES128,
                                          NULL,
                                          [data bytes],
                                          dataLength,
                                          buffer,
                                          bufferSize,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        NSData *resultData = [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
        //return [GTMBase64 stringByEncodingData:resultData];
        return [self hexStringFromData:resultData];
        
    }
    free(buffer);
    return nil;
}


// 普通字符串转换为十六进
+ (NSString *)hexStringFromData:(NSData *)data {
    Byte *bytes = (Byte *)[data bytes];
    // 下面是Byte 转换为16进制。
    NSString *hexStr = @"";
    for(int i=0; i<[data length]; i++) {
        NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i] & 0xff]; //16进制数
        newHexStr = [newHexStr uppercaseString];
        
        if([newHexStr length] == 1) {
            newHexStr = [NSString stringWithFormat:@"0%@",newHexStr];
        }
        
        hexStr = [hexStr stringByAppendingString:newHexStr];
        
    }
    return hexStr;
}


+(NSString *)URLEncodedString:(NSString *)URLPwd{
    NSString *encodedString = (NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)URLPwd, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8));
    return encodedString;
}

@end
